package com.diandian.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * @description CustomizedPropertyConfigurer
 * @author TDW
 * @date 2018年6月14日
 *
 */
public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {
	private static Map<String, String> ctxPropertiesMap;

	private static final Logger logger = LoggerFactory.getLogger(CustomizedPropertyConfigurer.class);
	public static boolean isBroadType = false;
	public static List<String> mqList = null;

	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		ctxPropertiesMap = new HashMap<String, String>();
		mqList = new ArrayList<String>();
		for (Object key : props.keySet()) {
			String keyStr = key.toString();
			String value = props.getProperty(keyStr);
			logger.info("加载配置项{}={}", key, value);
			ctxPropertiesMap.put(keyStr, value);
		}

	}

	/**
	 * @description getContextProperty
	 * @param key
	 * @return
	 * @author TDW
	 * @date 2018年6月14日
	 * @modify_date
	 * @modify_author
	 * @modify_desc
	 */
	public static String getContextProperty(String key) {
		return ctxPropertiesMap.get(key);
	}

	
	
	/**
	 * @description getContextProperty, 将property key对应的值转换成clazz值<br> 
	 * 支持的类型如下：<br>Integer,Long,Byte,Short,Float,Double,Boolean,String[], String[]按“,”切分，如：1,2,3
	 * @param key
	 * @param clazz
	 * @return
	 * @author TDW
	 * @date 2018年6月14日
	 * @modify_date 
	 * @modify_author
	 * @modify_desc
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getContextProperty(String key, Class<T> clazz) {
//		String stringValue = ctxPropertiesMap.get(key);
		String stringValue = key;//test
		if(Integer.class == clazz){
			return (T) new Integer(stringValue);
		}if(Long.class == clazz){
			return (T) new Long(stringValue);
		}if(Byte.class == clazz){
			return (T) new Byte(stringValue);
		}if(Short.class == clazz){
			return (T) new Short(stringValue);
		}if(Float.class == clazz){
			return (T) new Float(stringValue);
		}if(Double.class == clazz){
			return (T) new Double(stringValue);
		}if(Boolean.class == clazz){
			return (T) new Boolean(stringValue);
		}if(String[].class == clazz){
			return (T) stringValue.split(",");
		}else{
			throw new RuntimeException("不支持此类型："+clazz);
		}
		
		
		
	}

	
	
	/**
	 * @description main test
	 * @param args
	 * @author TDW
	 * @date 2018年6月14日
	 * @modify_date 
	 * @modify_author
	 * @modify_desc
	 */
	public static void main(String[] args) {
		System.out.println(getContextProperty("1", Integer.class)+", type="+getContextProperty("1", Integer.class).getClass());
		System.out.println(getContextProperty("1.0", Double.class)+", type="+getContextProperty("1.0", Double.class).getClass());
		System.out.println(getContextProperty("false", Boolean.class)+", type="+getContextProperty("false", Boolean.class).getClass());
		System.out.println(getContextProperty("1,2,3,4,5", String[].class)+", type="+getContextProperty("1,2,3,4,5", String[].class).getClass());
		System.out.println(getContextProperty("1,2,3,4,5", List.class)+", type="+getContextProperty("1,2,3,4,5", List.class).getClass());

	}

}
